package com.wsh.shop.back.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wsh.shop.common.pojo.ApiResponse;
import com.wsh.shop.common.servlet.BaseServlet;
import com.wsh.shop.common.util.PageInfoUtil;
import com.wsh.shop.users.pojo.AUsers;
import com.wsh.shop.users.service.IGoodsDbService;
import com.wsh.shop.users.service.IUsersDbService;
import com.wsh.shop.users.service.impl.GoodsDbServiceImpl;
import com.wsh.shop.users.service.impl.UsersDbServiceImpl;

/**
 * 第二个Servlet 只存储和管理员息息相关的信息 登陆后的操作
 * 
 * @author wangshMac
 */
@WebServlet(urlPatterns = "/back/UsersBackServlet")
public class UsersBackServlet extends BaseServlet
{
	private IUsersDbService usersDbService;
	private IGoodsDbService goodsDbService;
	
	@Override
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		UsersDbServiceImpl usersDbServiceImpl = new UsersDbServiceImpl();
		GoodsDbServiceImpl goodsDbServiceImpl = new GoodsDbServiceImpl();
		
		goodsDbServiceImpl.setUsersDbService(usersDbServiceImpl);
		usersDbServiceImpl.setGoodsDbService(goodsDbServiceImpl);
		
		this.goodsDbService = goodsDbServiceImpl ; 
		this.usersDbService = usersDbServiceImpl ; 
	}
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 接收参数 */
		String method = request.getParameter("method");
		if ("usersList".equalsIgnoreCase(method))
		{
			/* 用户列表 */
			usersList(request, response);
		} else if ("usersUpdate".equalsIgnoreCase(method))
        {
            /* 用户详情 */
            usersUpdate(request, response);
        } else if ("usersUpdateSubmit".equalsIgnoreCase(method))
        {
            /* 用户更新提交 */
            usersUpdateSubmit(request, response);
        } 
	}

	private void usersUpdateSubmit(HttpServletRequest request, HttpServletResponse response) {
	    ApiResponse<Object> dbApiResonse = new ApiResponse<Object>();
        /* 准备路径 */
        this.path = "/WEB-INF/jsp/back/users/usersInfo.jsp" ;
        Map<String, Object> condMap = new HashMap<String, Object>();
        
        /* 按照id查询 */
        String id = request.getParameter("id");
        condMap.put("id", id);
        ApiResponse<AUsers> apiResponse = this.usersDbService.findOneUsersService(condMap);
        request.setAttribute("response", apiResponse.toJSON());
        
        /* 根据不同的判断处理不同的类型 */
        String operType = request.getParameter("operType");
        AUsers dataOneObj = apiResponse.getDataOneJava();
        boolean updateFlag = false;
        if("update".equalsIgnoreCase(operType))
        {
           String status = request.getParameter("status");
           dataOneObj.setStatus(Byte.valueOf(status));
           updateFlag = true;
        }
        if (updateFlag) {
            /* 更新数据库 */
            dbApiResonse = this.usersDbService.updateOneUsersService(dataOneObj);
        }
        
        this.printOut(dbApiResonse.toJSON().toJSONString(), response);
    }

    /**
	 * 欢迎页面
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void usersUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		this.path = "/WEB-INF/jsp/back/users/usersInfo.jsp" ;
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		ApiResponse<AUsers> apiResponse = this.usersDbService.findOneUsersService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			this.path = "/WEB-INF/jsp/back/users/usersUpdate.jsp" ;
		}
		/* 页面跳转 */
		request.getRequestDispatcher(path).forward(request, response);
	}

	/**
	 * 用户列表
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void usersList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		this.path = "/WEB-INF/jsp/back/users/usersList.jsp" ; 
		
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<AUsers> apiResponse = this.usersDbService.findCondListUsersService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		request.getRequestDispatcher(path).forward(request, response);
	}
}
